package com.panda.pandawash.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager.LayoutParams;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.util.WsUtils;

/**
 * Created by gaowf on 2017/2/8.
 */

public class SetSafeDialog extends Dialog {
    private static String TAG = "API_TAG";
    public SetSafeDialog(Context context) {
        super(context);
    }

    public SetSafeDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private Button cancelButton;
        private Button confirmButton;
        private EditText firstEditText;
        private EditText confirmEditText;

        boolean setSuccess = false;
        private SetSafeDialog dialog = null;
        private String JUMPTOSAFEBOXACTIVITY = "com.panda.pandawash.SafeBoxActivity";

        public Builder(Context context) {
            this.context = context;
        }


        public SetSafeDialog create() {
            final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new SetSafeDialog(context, R.style.SetSafeDialogStyle);//SetSafeDialogStyle
            View layout = inflater.inflate(R.layout.dialog_set_safe, null);
            dialog.addContentView(layout, new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT
                    , android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);

            firstEditText = (EditText) layout.findViewById(R.id.first_psd);
            confirmEditText = (EditText) layout.findViewById(R.id.confirm_first_psd);

            cancelButton = (Button)  layout.findViewById(R.id.set_psd_cancel);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d("gwf_add", "click cancel in dialog");
                    dialog.dismiss();
                }
            });

            confirmButton = (Button) layout.findViewById(R.id.set_psd_confirm);
            confirmButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    comparePassword();
                   /* if (setSuccess == true) {
                        //final View progress = inflater.inflate(R.layout.dialog_progress, null);
                        //dialog.setContentView(progress);
                        //这里设置成功跳转界面或者其他逻辑
                        //TODO
                    } else {
                        //TODO
                    }*/
                }
            });
            return dialog;
        }

        protected void comparePassword() {
            String password1 = firstEditText.getText().toString().trim();
            String password2 = confirmEditText.getText().toString().trim();
            Log.d("hys123","getInputPsd "+ password1 +" getConfirmPsd " + password2);

            if (!WsUtils.isMatch(WsUtils.REGEX_STRONGPASSWORD, password1)) {
                showMessage(context.getString(R.string.safe_box_password_legal));
                setSuccess = false;
                return;
            }

            if (password1.equals(password2) && password2 != null && password1 != null) {
                setSuccess = true;
                setupStrongBoxPsw();

            } else {
                showMessage(context.getString(R.string.registe_password_not_same));
                setSuccess = false;
            }
        }


        private void showMessage(String string) {
            Toast toast = Toast.makeText(context.getApplicationContext(), string, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }

        private void setupStrongBoxPsw(){
            String account = CDApplication.getIntance().getUserInfo().getUseraccount();
            String password = firstEditText.getText().toString().trim();
            StrongBoxBean strongBoxBean = new StrongBoxBean();
            strongBoxBean.setSecretpassword(password);
            HttpUtil.setupStrongBoxPSW(new SafeBoxSetupCallback(),account,account,strongBoxBean);
        }

        private class SafeBoxSetupCallback extends AsyncHttpResponseHandler {
            @Override
            public void onSuccess(int statusCode, String content) {
                super.onSuccess(statusCode, content);
                Log.d(TAG, "SafeBoxSetupCallback statusCode = " + statusCode);
                Log.d(TAG, "SafeBoxSetupCallback onSuccess = " + content);
                if (statusCode == 200) {
                    HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                    Log.d(TAG, "resultBean = " + resultBean);
                    if (resultBean != null) {
                        String code = resultBean.getCode();
                        String message = resultBean.getMessage();
                        Log.d(TAG, "code = " + code);
                        if(code.equals("0")){
                            showMessage(context.getString(R.string.safe_box_password_setup_success));
                            Intent intent = new Intent();
                            intent.setAction(JUMPTOSAFEBOXACTIVITY);
                            intent.putExtra("msg", "发送确定进入保险箱广播成功！");
                            context.sendBroadcast(intent);
                        }else if(code.equals("99")){
                            showMessage(message);
                        }
                    }
                } else {
                    showMessage(context.getString(R.string.safe_box_password_setup_fail));
                }
                dialog.dismiss();
            }

            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);

                Log.d(TAG, "onFailure = " + error);
                if (error.toString().contains("refused")) {

                }
                dialog.dismiss();
            }
        }

    }
}
